package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSerialRule;
import com.ruoyi.system.service.ISysSerialRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单据规则Controller
 * 
 * @author huikai
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/system/billrule")
public class SysSerialRuleController extends BaseController
{
    @Autowired
    private ISysSerialRuleService sysSerialRuleService;

    /**
     * 查询单据规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:billrule:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSerialRule sysSerialRule)
    {
        startPage();
        List<SysSerialRule> list = sysSerialRuleService.selectSysSerialRuleList(sysSerialRule);
        return getDataTable(list);
    }

    /**
     * 导出单据规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:billrule:export')")
    @Log(title = "单据规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSerialRule sysSerialRule)
    {
        List<SysSerialRule> list = sysSerialRuleService.selectSysSerialRuleList(sysSerialRule);
        ExcelUtil<SysSerialRule> util = new ExcelUtil<SysSerialRule>(SysSerialRule.class);
        util.exportExcel(response, list, "单据规则数据");
    }

    /**
     * 获取单据规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:billrule:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId)
    {
        return success(sysSerialRuleService.selectSysSerialRuleByRuleId(ruleId));
    }

    /**
     * 新增单据规则
     */
    @PreAuthorize("@ss.hasPermi('system:billrule:add')")
    @Log(title = "单据规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSerialRule sysSerialRule)
    {
        return toAjax(sysSerialRuleService.insertSysSerialRule(sysSerialRule));
    }

    /**
     * 修改单据规则
     */
    @PreAuthorize("@ss.hasPermi('system:billrule:edit')")
    @Log(title = "单据规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSerialRule sysSerialRule)
    {
        return toAjax(sysSerialRuleService.updateSysSerialRule(sysSerialRule));
    }

    /**
     * 删除单据规则
     */
    @PreAuthorize("@ss.hasPermi('system:billrule:remove')")
    @Log(title = "单据规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds)
    {
        return toAjax(sysSerialRuleService.deleteSysSerialRuleByRuleIds(ruleIds));
    }

    /**
     * 获取单号
     */
    @GetMapping(value = "/ruleCode={ruleCode}")
    public AjaxResult generateSerialNumber(@PathVariable String ruleCode)
    {
        return success(sysSerialRuleService.generateSerialNumber(ruleCode));
    }
}
